<?php

namespace Tiger\Api\Traits;
use App\Models\Profile;
use Illuminate\Support\Facades\Hash;
use App\Transformers\ApiError;
trait RestfulFun
{
    public $parameters=[];

    public $resource_key='id';

    public  $models;

    public $trans;

    //****************************************************************
    //显示列表
    //GET  /res
    public function set_password_stub( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        //其他过滤条件
        if(is_callable($callback)) {
            $callback();
        }

        //校验旧密码
        $profile =Profile::getUser();
        if(!Hash::check($this->input->password,$profile->password )) {
            ApiError::errorUnauthorized('旧密码不正确');
        }
        $profile->password=bcrypt($this->input->new_password);
        $profile->save();
        return $this->apiReturnSuccess();
    }
    //GET  /res
    public function index_stub( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
       //其他过滤条件
        if(is_callable($callback)) {
            $callback();
        }
        //分页
       return $this->apiReturnPage($this->models,$this->trans);
    }

    public function index_stub_no_page( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        //其他过滤条件
        if(is_callable($callback)) {
            $callback();
        }
        //分页
        return $this->apiReturns($this->models,$this->trans);
    }
    //创建表单
    //GET /res/create
    public function create_stub( $request)
    {
        $this->Check($request,$this->parameters);
    }
    //编辑表单
    //GET /res/{res}/edit
    public function edit_stub($res, $request)
    {
        $this->Check($request,$this->parameters);
    }
    //获得指定的资源
    //GET  /res/{res}
    public function show_stub( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        //其他过滤条件
        if(is_callable($callback)) {
            $callback();
        }
        return $this->apiReturn($this->models,$this->trans);
    }

    //获得指定的资源
    //GET  /res/{res}
    public function show_stub_list( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        //其他过滤条件
        if(is_callable($callback)) {
            $callback();
        }

        return $this->apiReturns($this->models,$this->trans);
    }
    //****************************************************************

    //存储
    //POST /res
    public function store_stub( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        if(is_callable($callback)) {
            $callback();
        }
        return $this->apiReturn($this->models,$this->trans);
    }
    //更新资源
    //PUT/PATCH  /res/{res}
    public function update_stub( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        if(is_callable($callback)) {
            $callback();
        }
        return $this->apiReturn($this->models,$this->trans);
    }
    //移除资源
    //DELETE /res/{res}
    public function destroy_stub($request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        if(is_callable($callback)) {
            $callback();
        }
        return $this->apiReturnSuccess();
    }
    //****************************************************************

    public function single_data_stub( $request,$callback=null)
    {
        $this->Check($request,$this->parameters);
        if(is_callable($callback)) {
            $callback();
        }
        return $this->apiReturnSingle($this->models);
    }
}
